<?php
namespace Poi\EttsBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Poi\EttsBundle\Model\Ticket;
use Poi\EttsBundle\Model\Tickets;

/**
 * @author Ricardo J. Vazquez
 * 
 * Controller class to handle requests for Tickets
 */
class TicketController extends Controller
{
	private $conn = null; //Connection
	
	/**
	* Returns response with either a single or collection of Ticket models.
	*
	* @param $number  number of the ticket
	* @return Response with collection or model of Ticket in xml format
	*/
    public function indexAction($number){
		$this->conn = $this->get('doctrine.dbal.default_connection');
		
		if($number != 'nil'){
			$t = new Ticket($this->conn, $number);	
			
			$r = new Response($t->toXML());
			$r->headers->set('Content-Type', 'text/xml');
			
			return $r;
		}
		
		$ts = new Tickets($this->conn);
		$ts->setLoadCallback('loadTickets','Ticket',$ts);
		
		$r = new Response($ts->toXML());
		$r->headers->set('Content-Type', 'text/xml');
		
		return $r;
    }

	/**
	* Returns response with status of the write
	*
	* @param $number  number of the ticket
	* @return Response with status of the write in xml format
	*/
    public function newAction($number){
		$this->conn = $this->get('doctrine.dbal.default_connection');
		$request = $this->get('request');
				
		if($number != 'nil'){
			$t = new Ticket($this->conn, $number);	
			
			($request->request->get('type') == ''? false : $t->setType($request->request->get('type')) );
			($request->request->get('officer') == '' ? false : $t->setOfficer($request->request->get('officer')) );
			($request->request->get('owner') == '' ? false : $t->setOwner($request->request->get('owner')) );
			($request->request->get('infraction') == '' ? false : $t->setInfractionKind($request->request->get('infraction')) );
			($request->request->get('longitude') == '' ? '' : $t->setLongitude($request->request->get('longitude')) );
			($request->request->get('latitude') == '' ? '' : $t->setLatitude($request->request->get('latitude')) );
			($request->request->get('time') =='' ? '' : $t->setTime($request->request->get('time')) );
			if($request->request->get('type') == 'driver'){
				($request->request->get('observations') =='' ? '' : $t->setObservations($request->request->get('observations')) );
			} 
			
			// $t->setLongitude(-67.02312);			
			// $t->setLatitude(18.17211);			
			// $t->setInfractionKind('Stepped on my foot');	
			// $t->setType('driver');
			// $t->setTime('2011-01-31 19:46:00');
			// $t->setOfficer(123456);
			// $t->setOwner(1234567);
			// $t->setObservations('No vio el radar,No me hizo caso');
			
			$r = new Response($t->create());
			$r->headers->set('Content-Type', 'text/xml');
			
			return $r;
		}		
		else {
			$t = new Ticket($this->conn);	
			
			($request->request->get('type') == '' ? false : $t->setType($request->request->get('type')) );
			($request->request->get('officer') == '' ? false : $t->setOfficer($request->request->get('officer')) );
			($request->request->get('owner') == '' ? false : $t->setOwner($request->request->get('owner')) );
			($request->request->get('infraction') == '' ? false : $t->setInfractionKind($request->request->get('infraction')) );
			($request->request->get('longitude') == '' ? '' : $t->setLongitude($request->request->get('longitude')) );
			($request->request->get('latitude') == '' ? '' : $t->setLatitude($request->request->get('latitude')) );
			($request->request->get('time') == '' ? '' : $t->setTime($request->request->get('time')) );
			if($request->request->get('type') == 'driver'){
				($request->request->get('observations') =='' ? '' : $t->setObservations($request->request->get('observations')) );
			}
			
			$r = new Response($t->create());
			$r->headers->set('Content-Type', 'text/xml');
			
			return $r;
		}
    }
	
	/**
	* Returns response with status of the modify
	*
	* @param $number  number of the ticket
	* @return Response with status of the modify in xml format
	*/
	public function updateAction($number) {
		$this->conn = $this->get('doctrine.dbal.default_connection');
		$request = $this->get('request');
		
		//Marronaso para PUT
		parse_str(file_get_contents('php://input'), $put_vars);
		$request = $request->duplicate(null, $put_vars);		
		
		$t = new Ticket($this->conn, $number);	
				
		($request->request->get('officer') == '' ? '' : $t->updOfficer($request->request->get('officer')) );
		($request->request->get('owner') == '' ? '' : $t->updOwner($request->request->get('owner')) );
		($request->request->get('infraction') == '' ? '' : $t->updInfractionKind($request->request->get('infraction')) );
		($request->request->get('longitude') == '' ? '' : $t->updLongitude($request->request->get('longitude')) );
		($request->request->get('latitude') == '' ? '' : $t->updLatitude($request->request->get('latitude')) );
		($request->request->get('time') == '' ? '' : $t->updTime($request->request->get('time')) );
		($request->request->get('observations') == '' ? '' : $t->updObservations($request->request->get('observations')) );
		
		$r = new Response($t->update());
		$r->headers->set('Content-Type', 'text/xml');
		
		return $r;
	}
	
	/**
	* Returns response with status of the delete
	*
	* @param $number  number of the ticket
	* @return Response with status of the delete in xml format
	*/
	public function removeAction($number) {
		$this->conn = $this->get('doctrine.dbal.default_connection');
		
		$t = new Ticket($this->conn, $number);	
		
		$r = new Response($t->delete());
		$r->headers->set('Content-Type', 'text/xml');
		
		return $r;
	}
}